<?php

class Backend extends TST_Controller {

    protected $template = 'admin';

    function Backend() {
        parent::__construct();
        $this->output->enable_profiler(PROFILER);
        $this->lang->load('menu', 'english');
        require_once(APPPATH . '/libraries/app-constants.php');
        // Default sidebars
        //Default keyword search;
        $this->data['keywords'] = '';

        // Default css
        $this->layout[AppContants::LAYOUT_CSS] = array('admin/bootstrap.min', 'admin/font-awesome.min', 'admin/ionicons.min', 'admin/morris/morris', 'admin/jvectormap/jquery-jvectormap-1.2.2', 'admin/fullcalendar/fullcalendar', 'admin/daterangepicker/daterangepicker-bs3', 'admin/bootstrap-wysihtml5/bootstrap3-wysihtml5.min', 'admin/AdminLTE', 'admin/style');
        //Default js
        $this->layout[AppContants::LAYOUT_JS] = array();
        // Breadcrumb
        $this->layout[AppContants::BREADCRUMB] = NULL;

        //Default active menu top
        $this->layout[AppContants::ACTIVE_MENU] = "";
        // Default meta header info
        $this->meta[AppContants::META_TITLE] = '';
        $this->meta[AppContants::META_KEYWORDS] = '';
        $this->meta[AppContants::META_DESCRIPTION] = '';
    }

    public function index() {
        
    }

    function getMenu($menu, $parentid = 0, $level = 1, $space = '', &$newArr) {
        if (!empty($menu)) {
            foreach ($menu as $key => $info) {
                if ($info['parent_id'] == $parentid) {
                    $info['level'] = $level;
                    $info['name'] = $space . '&nbsp;&nbsp;&nbsp;' . $info['name'];
                    $newArr[] = $info;
                    $newparentid = $info['id'];
                    unset($menu[$key]);
                    $this->recursive($menu, $newparentid, $level + 1, $space . '&gt', $newArr);
                }
            }
        }
    }

    function recursive($menu, $parentid = 0, $level = 1, $space = '', &$newArr) {
        if (!empty($menu)) {
            foreach ($menu as $key => $info) {
                if ($info['parent_id'] == $parentid) {
                    $info['level'] = $level;
                    $info['name'] = $space . '&nbsp;&nbsp;&nbsp;' . $info['name'];
                    $newArr[] = $info;
                    $newparentid = $info['id'];
                    unset($menu[$key]);
                    $this->recursive($menu, $newparentid, $level + 1, $space . $space, $newArr);
                }
            }
        }
    }

    function Menu1($menu, $parentid = 0, &$str) {
        if (!empty($menu)) {
            if ($parentid == 0)
                $str .= '<ul class="nav navbar-nav">';
            else
                $str .= '<ul>';
            foreach ($menu as $key => $info) {
                if ($info['parent_id'] == $parentid) {
                    $class = 'class="level-' . $info['level'] . '"';
                    $str.= '<li ' . $class . '><a href="' . base_url($info['friendly_url']) . '">' . $info['name'] . '</a>';
                    $newparentid = $info['id'];
                    unset($menu[$key]);
                    $this->Menu1($menu, $newparentid, $str);
                    $str.='</li>';
                }
            }
            $str.= '</ul>';
        }
    }

    public function updateStatus($id, $model, $status) {
        $data = array(
            'status' => $status
        );
        $this->$model->update($id, $data);
    }

    public function createBreadcrunb($page = null, $url = null) {
        $breadcrumb = array();
        $exp_page = explode("/", $page);
        $exp_url = explode("/", $url);
        $breadcrumb[0]['page'] = "";
        $breadcrumb[0]['url'] = "";
        if (count($exp_page) > 1) {
            for ($i = 1; $i < count($exp_url); $i++) {
                $breadcrumb[$i]['page'] = $exp_page[$i];
                $breadcrumb[$i]['url'] = $breadcrumb[$i - 1]['url'] . "/" . $exp_url[$i];
            }
        }
        return $breadcrumb;
    }

    function getListForSelect($parentid = 0, $space = '--', $trees = NULL) {
        if (!$trees)
            $trees = array();
        $sql = mysql_query("SELECT id, name FROM category WHERE status <> 'delete' AND parent_id = " . intval($parentid));
        while ($rs = mysql_fetch_assoc($sql)) {
            $trees[] = array('id' => $rs['id'], 'name' => $space . '&nbsp' . $rs['name']);
            $trees = $this->getListForSelect($rs['id'], $space . '--', $trees);
        }
        return $trees;
    }

    public function getNavigation() {
        $level = $this->Mcategory->getLevel();
        $category['level1'] = $this->Mcategory->getCategoryByLevelADMIN($level[0]['level']);
        for ($i = 1; $i < count($level); $i++) {
            for ($j = 0; $j < count($category['level' . $i]); $j++) {
                $t = $this->Mcategory->getCategoryByLevelADMIN($level[$i]['level'], $category['level' . $i][$j]['id']);
                if (!empty($t))
                    $category['level' . $i][$j]['level' . ($i + 1)] = $t;
            }
        }

        $data['category'] = $category;
        $data['level'] = $level;

        return $data;
    }

    public function upload($path = NULL, $name = NULL) {
        $config['upload_path'] = $path;
        $config['allowed_types'] = '*';

        $this->load->library('upload', $config);

        if ($_FILES[$name]['name'] != '') {
            if (!$this->upload->do_upload($name)) {
                echo $this->upload->display_errors('<p style="color: #f00;">', '</p>');
                exit();
                return '';
            } else {
                $data = $this->upload->data();
                return $data['file_name'];
            }
        }
        return '';
    }

    public function print_debug($var) {
        echo '<pre>';
        print_r($var);
        echo '</pre>';
        die;
    }

}

?>
